//
//  HMMainTabBarController.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/17.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMMainTabBarController.h"
#import "HMHomeViewController.h"
#import "HMComposeViewController.h"
#import "HMMessageViewController.h"
#import "HMProfileViewController.h"
#import "HMDiscoverViewController.h"
#import "UIImage+MJ.h"
#import "HMTabBar.h"
#import "HMNavigationViewController.h"
#import <SVProgressHUD.h>

#import "HMUserAccountViewModel.h"
#import "HMComposeView.h"
#import "HMGlobalConst.h"

@interface HMMainTabBarController ()<HMTabBarDelegate>

/**
 *  自定义的tabbar
 */
@property (nonatomic, weak) HMTabBar *customTabBar;

@end

@implementation HMMainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     [self setupTabbar];
       //1.添加子控制器
    self.tabBar.tintColor = [UIColor orangeColor];

//    [self addChildViewController:vc];
    [self setupAllChildViewControllers];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 删除系统自动生成的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
    
    //
//    self.tabBar setSelectedItem:<#(UITabBarItem * _Nullable)#>
    
//    self.selectedIndex = 0;

}

/**
 *  初始化tabbar
 */
- (void)setupTabbar
{
    HMTabBar *customTabBar = [[HMTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    customTabBar.delegate = self;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}

/**
 *  监听tabbar按钮的改变
 *  @param from   原来选中的位置
 *  @param to     最新选中的位置
 */
- (void)tabBar:(HMTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to
{
    self.selectedIndex = to;
    NSLog(@"%zd",to);
    if (![HMUserAccountViewModel sharedViewModel].isLogin) {
        [SVProgressHUD showErrorWithStatus:@"请先登录,亲~"];
    }
    
    HMComposeView *composeView = [[HMComposeView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
//    UIWindow *currentWindow = [UIApplication sharedApplication].windows.lastObject;
//    [self.view addSubview:composeView];
    
    [composeView showComposeView:self];
}

- (void)setupAllChildViewControllers
{
    // 1.首页
    HMHomeViewController *home = [[HMHomeViewController alloc] init];
    home.tabBarItem.badgeValue = @"N";
    [self setupChildViewController:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_highlighted"];
    
    // 2.消息
    HMMessageViewController *message = [[HMMessageViewController alloc] init];
    message.tabBarItem.badgeValue = @"8";
    [self setupChildViewController:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_highlighted"];
    
    // 3.广场
    HMDiscoverViewController *discover = [[HMDiscoverViewController alloc] init];
    discover.tabBarItem.badgeValue = @"7";
    [self setupChildViewController:discover title:@"广场" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_highlighted"];
    
    // 4.我
    HMProfileViewController *me = [[HMProfileViewController alloc] init];
    me.tabBarItem.badgeValue = @"90";
    [self setupChildViewController:me title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_highlighted"];
}

/**
 *  初始化一个子控制器
 *
 *  @param childVc           需要初始化的子控制器
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.设置控制器的属性
    childVc.title = title;
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageWithName:imageName];
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageWithName:selectedImageName];
//    if (iOS7) {
//        childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    } else {
        childVc.tabBarItem.selectedImage = selectedImage;
//    }
    
    // 2.包装一个导航控制器
    UINavigationController *nav = [[HMNavigationViewController alloc] initWithRootViewController:childVc];
//    nav.title = title;
    [self addChildViewController:nav];
    
    // 3.添加tabbar内部的按钮
    [self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
}

@end
